-- @owner: @zou_jialiang050
-- @date: 2022/02/24
-- @testpoint: 在从src视图中删除数据时同步将src表中相应数据删除，并在之后向des表中插入计数器数字 each statement

--step1:建表;expect:成功
drop table if exists t_trigger_src_0050 cascade;
drop table if exists t_trigger_des_0050 cascade;
drop sequence if exists trigger_des_seq1 cascade;
create table t_trigger_src_0050(id1 int, id2 int, id3 int) with (storage_type=ustore);
create table t_trigger_des_0050(id1 int, id2 int, id3 int) with (storage_type=ustore);

--step2:在从src视图中删除数据时同步将src表中相应数据删除，并在之后向des表中插入计数器数字 each statement;expect:成功
create or replace view t_trigger_src_0050_view1 as select * from t_trigger_src_0050;
create sequence trigger_des_seq1;
create or replace function tri_truncate_func() returns trigger as
$$
declare
begin
insert into t_trigger_des_0050 values(nextval('trigger_des_seq1'),nextval('trigger_des_seq1'),nextval('trigger_des_seq1'));
return new;
end
$$ language plpgsql;
/
drop trigger if exists truncate_trigger on t_trigger_src_0050_view1;
create trigger truncate_trigger after delete on t_trigger_src_0050_view1
for each statement
execute procedure tri_truncate_func();
/
create or replace function delete_func()
returns trigger as $$
begin
delete from t_trigger_src_0050 where id3<3;
return new;
end;
$$language plpgsql;
/
drop trigger if exists delete_trigger_1 on t_trigger_src_0050_view1;
create trigger delete_trigger_1
instead of delete on t_trigger_src_0050_view1
for each row
execute procedure delete_func();
/
insert into t_trigger_src_0050 values(1,1,1),(2,2,2),(3,3,3);
select * from t_trigger_src_0050 order by id1;
select * from t_trigger_des_0050 order by id1;
delete from t_trigger_src_0050_view1 where id3<3;
select * from t_trigger_src_0050 order by id1;
select * from t_trigger_des_0050 order by id1;

--step3:清理环境;expect:成功
drop table if exists t_trigger_src_0050 cascade;
drop table if exists t_trigger_des_0050 cascade;
drop sequence if exists trigger_des_seq1 cascade;